package com.wulin;

import com.wulin.mapper.HarvestDataMapper;
import com.wulin.mapper.PestDiseaseDataMapper;
import com.wulin.mapper.UserDataMapper;
import com.wulin.pojo.HarvestData;
import com.wulin.pojo.PestDiseaseData;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.io.IOException;
import java.io.InputStream;

public class details_two extends JFrame {
	private JPanel contentPane;
	JTextField textField, textField_1;
	JTextField textField_3;
	private JLabel lblNewLabel_4;
	JTextField textField_5;
	private JLabel lblNewLabel_5;
	JTextField textField_6;

	JTextField textField_4;

	private JButton btnNewButton;
	String resource = "mybatis-config.xml";
	InputStream inputStream = Resources.getResourceAsStream(resource);
	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

	SqlSession sqlSession =sqlSessionFactory.openSession();
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					details_one frame = new details_one();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public details_two() throws IOException {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1402, 823);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel_2 = new JLabel("UID:");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 30));
		lblNewLabel_2.setBounds(233, 8, 457, 75);
		contentPane.add(lblNewLabel_2);
		
		textField = new JTextField();
		textField.setFont(new Font("宋体", Font.PLAIN, 26));
		textField.setBounds(690, 8, 414, 75);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("agricultural_product_variety:");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 30));
		lblNewLabel.setBounds(233, 83, 457, 75);
		contentPane.add(lblNewLabel);
		
		textField_1 = new JTextField();
		textField_1.setFont(new Font("宋体", Font.PLAIN, 26));
		textField_1.setBounds(690, 83, 414, 75);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("planting_area:");
		lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 30));
		lblNewLabel_3.setBounds(233, 158, 457, 75);
		contentPane.add(lblNewLabel_3);
		
		textField_3 = new JTextField();
		textField_3.setFont(new Font("宋体", Font.PLAIN, 26));
		textField_3.setBounds(690, 158, 414, 75);
		contentPane.add(textField_3);
		textField_3.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("yield:");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 30));
		lblNewLabel_1.setBounds(233, 233, 457, 75);
		contentPane.add(lblNewLabel_1);
		
		textField_6 = new JTextField();
		textField_6.setFont(new Font("宋体", Font.PLAIN, 26));
		textField_6.setBounds(690, 233, 414, 75);
		contentPane.add(textField_6);
		textField_6.setColumns(10);
		
		lblNewLabel_5 = new JLabel("harvest_time:");
		lblNewLabel_5.setFont(new Font("宋体", Font.PLAIN, 30));
		lblNewLabel_5.setBounds(233, 308, 457, 75);
		contentPane.add(lblNewLabel_5);
		
		textField_5 = new JTextField();
		textField_5.setFont(new Font("宋体", Font.PLAIN, 26));
		textField_5.setBounds(690, 308, 414, 75);
		contentPane.add(textField_5);
		textField_5.setColumns(10);
		
		lblNewLabel_4 = new JLabel("plant_area:");
		lblNewLabel_4.setFont(new Font("宋体", Font.PLAIN, 30));
		lblNewLabel_4.setBounds(233, 383, 457, 75);
		contentPane.add(lblNewLabel_4);
		
		textField_4 = new JTextField();
		textField_4.setFont(new Font("宋体", Font.PLAIN, 26));
		textField_4.setBounds(690, 383, 414, 75);
		contentPane.add(textField_4);
		textField_4.setColumns(10);
		
		btnNewButton = new JButton("删除");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					int id = Integer.parseInt(textField.getText());
					HarvestDataMapper harvestDataMapper = sqlSession.getMapper(HarvestDataMapper.class);
					int rows = harvestDataMapper.delete(id);
					if (rows > 0) {
						JOptionPane.showMessageDialog(details_two.this, "删除成功", "成功", JOptionPane.CLOSED_OPTION);
					} else {
						JOptionPane.showMessageDialog(details_two.this, "删除失败", "失败", JOptionPane.ERROR_MESSAGE);
					}
					sqlSession.commit();
				} catch (Exception ex) {
					ex.printStackTrace();
					JOptionPane.showMessageDialog(details_two.this, "操作失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
					sqlSession.rollback();
				}
//				Integer id =Integer.valueOf(textField.getText());
//				String sql = "DELETE FROM harvest_data WHERE UID = " + id + " ";
//				if(db.executeUpdate(sql)){
//					JOptionPane.showMessageDialog(details_two.this, "删除成功","成功", JOptionPane.CLOSED_OPTION);
//				}
//				else{
//					JOptionPane.showMessageDialog(details_two.this, "删除失败","失败", JOptionPane.ERROR_MESSAGE);
//				}
//				setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			}
		});
		btnNewButton.setBounds(233, 582, 305, 75);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("确认更改");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				try {
					int id = Integer.parseInt(textField.getText());
					String agriculturalProductVariety = textField_1.getText();
					String plantingArea = textField_3.getText();
					String yield = textField_6.getText();
					String harvestTime = textField_5.getText();
					String plantArea = textField_4.getText();

					// 创建 HarvestData 对象
					HarvestData harvestData = new HarvestData();
					harvestData.setUID(id);
					harvestData.setAgricultural_product_variety(agriculturalProductVariety);
					harvestData.setPlanting_area(plantingArea);
					harvestData.setYield(yield);
					harvestData.setHarvest_time(harvestTime);
					harvestData.setPlant_area(plantArea);

					// 获取 Mapper 接口
					HarvestDataMapper harvestDataMapper = sqlSession.getMapper(HarvestDataMapper.class);

					// 执行更新操作
					int rows = harvestDataMapper.update(harvestData);
					if (rows > 0) {
						JOptionPane.showMessageDialog(details_two.this, "修改成功", "成功", JOptionPane.CLOSED_OPTION);
					} else {
						JOptionPane.showMessageDialog(details_two.this, "修改失败", "失败", JOptionPane.ERROR_MESSAGE);
					}
					sqlSession.commit(); // 提交事务
				} catch (Exception ex) {
					ex.printStackTrace();
					JOptionPane.showMessageDialog(details_two.this, "操作失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
					sqlSession.rollback(); // 回滚事务
				}
//				Integer id =Integer.valueOf(textField.getText());
//				String apty = textField_1.getText();
//				String pgaa = textField_3.getText();
//				String yield = textField_6.getText();
//				String htte = textField_5.getText();
//				String ptaa = textField_4.getText();
//				String sql = "UPDATE harvest_data SET agricultural_product_variety = '"+ apty +"' ,"
//						+ " planting_area = "+ pgaa +" ,"
//							+ " yield = "+ yield +" ,"
//								+ " harvest_time = '"+ htte + "' ,"
//									+ " plant_area = '"+ ptaa + "'"
//										+ " WHERE UID = " + id + "";


			}
		});
		btnNewButton_1.setBounds(799, 582, 305, 75);
		contentPane.add(btnNewButton_1);
	}
}
